// ==========================================================================
// gin-sns自动生成业务逻辑层相关代码，只生成一次，按需修改,再次生成不会覆盖.
// date：2021-02-21 13:45:53 +0800 CST
// path: app/service/module/category_service.go
// author：jon
// ==========================================================================
package module

import (
	"errors"
	"gin-sns/app/dao/module/category"
	"gin-sns/app/model"
	"gin-sns/app/model/constants"
	categoryModel "gin-sns/app/model/module/category"
	"time"

	"github.com/gin-gonic/gin"

	"gin-sns/app/ginframe/utils/convert"
	"gin-sns/app/ginframe/utils/page"
)

var CategoryService = newCategoryService()

func newCategoryService() *categoryService {
	return &categoryService{}
}

type categoryService struct {
}

//根据主键查询数据
func (s *categoryService) SelectRecordById(id int64) (*categoryModel.Entity, error) {
	return category.CategoryDao.SelectById(id)
}

//
func (s *categoryService) SelectRecordsIn(idarr []int64) ([]categoryModel.Entity, error) {

	list, err := category.CategoryDao.FindIn("id", idarr)
	if list != nil && len(list) > 0 {
		err = errors.New("not found")
		list = nil
	}
	return list, err
}

//根据主键删除数据
func (s *categoryService) DeleteRecordById(id int64) bool {
	return category.CategoryDao.DeleteById(id)
}

//批量删除数据记录
func (s *categoryService) DeleteRecordByIds(ids string) int64 {
	ida := convert.ToInt64Array(ids, ",")
	result, err := category.CategoryDao.DeleteBatch(ida...)
	if err != nil {
		return 0
	}
	return result
}

//添加数据
func (s *categoryService) AddSave(req *categoryModel.AddReq, ctx *gin.Context) (int64, error) {
	var entity categoryModel.Entity

	//entity.Id = req.Id
	entity.Type = req.Type
	entity.Title = req.Title
	entity.Description = req.Description
	entity.Status = req.Status
	entity.CreateTime = time.Now()
	//entity.CreateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user != nil {
	//	entity.CreateBy = user.LoginName
	//}

	_, err := category.CategoryDao.Insert(&entity)
	return entity.Id, err
}

//修改数据
func (s *categoryService) EditSave(req *categoryModel.EditReq, ctx *gin.Context) (int64, error) {
	entity := &categoryModel.Entity{Id: req.Id}
	ok, err := category.CategoryDao.FindOne(entity)

	if err != nil {
		return 0, err
	}

	if !ok {
		return 0, errors.New("数据不存在")
	}

	entity.Type = req.Type
	entity.Title = req.Title
	entity.Description = req.Description
	entity.Status = req.Status
	//entity.UpdateTime = time.Now()
	//entity.UpdateBy = ""

	//user := system.UserService.GetProfile(ctx)

	//if user == nil {
	//	entity.UpdateBy = user.LoginName
	//}

	return category.CategoryDao.Update(entity)
}

//根据条件查询数据
func (s *categoryService) SelectListAll(params *categoryModel.SelectPageReq) ([]categoryModel.Entity, error) {
	return category.CategoryDao.SelectListAll(params)
}

//根据条件分页查询数据
func (s *categoryService) SelectListByPage(params *categoryModel.SelectPageReq) ([]categoryModel.Entity, *page.Paging, error) {
	return category.CategoryDao.SelectListByPage(params)
}

// 导出excel
func (s *categoryService) Export(param *categoryModel.SelectPageReq) (string, error) {
	head := []string{"id", "类型", "标题", "描述", "0未审核 1正常状态 2审核未通过 3删除", "创建时间"}
	col := []string{"id", "type", "title", "description", "status", "create_time"}
	return category.CategoryDao.SelectListExport(param, head, col)
}

func (s *categoryService) GetOrCreate(name string) (*categoryModel.Entity, error) {
	entity := s.FindByName(name)
	if entity != nil {
		return entity, nil
	} else {
		entity = &categoryModel.Entity{
			Title:      name,
			Type:       constants.EntityTypeArticle,
			Status:     model.CategoryStatusOk,
			CreateTime: time.Now(),
			//UpdateTime: time.Now(),
		}
		_, err := category.CategoryDao.Insert(entity)
		if err != nil {
			return nil, err
		}
		return entity, nil
	}
}

func (s *categoryService) FindByName(name string) *categoryModel.Entity {
	if len(name) == 0 {
		return nil
	}

	entity := &categoryModel.Entity{Title: name}
	ok, err := category.CategoryDao.FindOne(entity)
	if !ok || err != nil {
		return nil
	}
	return entity
}

//func GetCategories() []categoryModel.Entity {
//return categoryModel.GetCategories()
//}

func (s *categoryService) GetNewestCategories(t int, limit int, start int) ([]categoryModel.Entity, error) {

	return category.CategoryDao.GetNewestCategories(t, limit, start)
}
